//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.couponmanagecl;

import com.google.common.base.Strings;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.alarm.alarmclock.AlarmClock;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.definitions.PageForm;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.commonunits.utils.SnowFlake;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basicdata.StringMap;
import com.nebutown.generation.basictype.UID;
import com.nebutown.generation.basictype.UIDLists;
import com.restaurant.application.coupon.Coupon;
import com.restaurant.application.coupon.support.CouponData;
import com.restaurant.application.couponmanage.support.CouponManageData;
import com.restaurant.application.couponmanage.support.CreatePateTypeData;
import com.restaurant.application.couponmanagecl.support.AbstractCouponManageCL;
import com.restaurant.application.indexes.couponlist.mysql.CouponListBean;
import com.restaurant.application.indexes.couponlist.mysql.CouponListCriteria;
import com.restaurant.application.indexes.couponlist.mysql.CouponListImpl;
import com.restaurant.application.indexes.couponmanagelist.AssistCouponManageList;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListBean;
import com.restaurant.application.indexes.couponmanagelist.mysql.CouponManageListImpl;
import com.restaurant.application.util.CouponManageUtils;
import java.util.List;
import java.util.Objects;

public class CouponManageCLImpl extends AbstractCouponManageCL {
    public CouponManageCLImpl() {
    }

    private String getStr(CouponManageData data) {
        if (!Objects.isNull(data.getFullMoney()) && data.getFullMoney() > 0.0D) {
            if (!Objects.isNull(data.getMinusMoney()) && data.getMinusMoney() > 0.0D) {
                if (Strings.isNullOrEmpty(data.getModality())) {
                    return "请填写券名!";
                } else if (!Objects.isNull(data.getTotalAmount()) && data.getTotalAmount() > 0L) {
                    if (!Objects.isNull(data.getReceiveTime()) && data.getReceiveTime() != 0L) {
                        if (!Objects.isNull(data.getEndTime()) && data.getEndTime() != 0L) {
                            if (data.getEndTime() < DateTimeUtils.getCurrentMillisecond()) {
                                return "请选择正确的使用时间";
                            } else {
                                return !Objects.isNull(data.getShopNameList()) && data.getShopNameList().size() != 0 ? null : "请选择门店";
                            }
                        } else {
                            return "请填写使用结束时间!";
                        }
                    } else {
                        return "请填写使用开始时间!";
                    }
                } else {
                    return "请填写发行量!";
                }
            } else {
                return "请填写减去多少元";
            }
        } else {
            return "请填写满多少元";
        }
    }

    public ViewUpdate save(CouponManageData data, int[] seq) {
        String msg = this.getStr(data);
        if (!Strings.isNullOrEmpty(msg)) {
            return this.returnViewCall(this.buildViewCall(this.unitPosition(), "showErrorMessage", (List)null, (new StringMap()).putData("errorMessage", msg)));
        } else {
            this.perfect(data);
            if (Objects.isNull(this.getData().getCreateTime())) {
                this.getData().withId(SnowFlake.findUniqueId()).withCreateTime(DateTimeUtils.getCurrentMillisecond()).withState("有效").withNumber(CouponManageUtils.getNumber("CL", this)).withType("满减券").withAmount(0L);
                this.setAlarmClock();
                CouponManageListBean bean = ((AssistCouponManageList)this.getSingleton(AssistCouponManageList.class)).createBean(this.getId(), this.getData());
                ((CouponManageListImpl)this.getIndex(CouponManageListImpl.class)).insert(bean);
            }

            this.quickIndexUpdate();
            return this.returnPopFromContainer(this.getId(), false);
        }
    }

    private void setAlarmClock() {
        long time = (this.getData().getEndTime() - DateTimeUtils.getCurrentMillisecond()) / 1000L;
        ((AlarmClock)this.getSingleton(AlarmClock.class)).setDelayAlarmClock(this.getData().getId() + this.getData().getNumber(), time, this.getId());
    }

    public ViewUpdate cancel(int[] seq) {
        return this.returnPopFromContainer(this.getId(), false);
    }

    private void perfect(CouponManageData data) {
        this.getData().withModifyTime(DateTimeUtils.getCurrentMillisecond());
        if (!Objects.equals(data.getEndTime(), this.getData().getEndTime()) || !Objects.equals(data.getRemarks(), this.getData().getRemarks()) || !Objects.equals(data.getShopNameList(), this.getData().getShopNameList())) {
            CouponManageUtils.modifyCoupon(this, data, this.getId());
        }

        if (Objects.nonNull(data.getFullMoney()) && data.getFullMoney() > 0.0D) {
            this.getData().withFullMoney(data.getFullMoney());
        }

        if (Objects.nonNull(data.getMinusMoney()) && data.getMinusMoney() > 0.0D) {
            this.getData().withMinusMoney(data.getMinusMoney());
        }

        if (!Strings.isNullOrEmpty(data.getModality())) {
            this.getData().withModality(data.getModality());
        }

        if (Objects.nonNull(data.getTotalAmount()) && data.getTotalAmount() > 0L) {
            this.getData().withTotalAmount(data.getTotalAmount());
        }

        if (Objects.nonNull(data.getEndTime())) {
            this.getData().withEndTime(data.getEndTime());
            if (Objects.nonNull(this.getData().getCreateTime())) {
                this.setAlarmClock();
            }
        }

        if (Objects.nonNull(data.getReceiveTime())) {
            this.getData().withReceiveTime(data.getReceiveTime());
        }

        if (!Strings.isNullOrEmpty(data.getRemarks())) {
            this.getData().withRemarks(data.getRemarks());
        }

        if (Objects.nonNull(data.getShopNameList()) && data.getShopNameList().size() > 0) {
            this.getData().withShopNameList(data.getShopNameList()).withShopList(CouponManageUtils.getShopListData(this, data.getShopNameList()));
        }

        if (!Strings.isNullOrEmpty(data.getState())) {
            this.getData().withState(data.getState());
        }

        if (Objects.nonNull(data.getLimitId()) && data.getLimitId() > 0L) {
            this.getData().withLimitId(data.getLimitId());
        }

        if (Objects.nonNull(data.getLimitAccount()) && data.getLimitAccount() > 0L) {
            this.getData().withLimitAccount(data.getLimitAccount());
        }

    }

    public View getFullView() {
        return !Objects.isNull(this.getData().getCreateTime()) && this.getData().getCreateTime() != 0L ? this.newCommonTable(DataModes.SHOW) : this.newCommonTable(DataModes.EDIT);
    }

    public View getViewLead(ViewSpec viewSpec) {
        return this.getFullView();
    }

    public UIDLists batchCreateCoupon(int amount) {
        UIDLists uidLists = CouponManageUtils.batchCreateCoupon(amount, this, this.getData(), this.getId());
        if (Objects.nonNull(uidLists)) {
            long newAmount = this.getData().getAmount() + (long)amount;
            this.getData().withAmount(newAmount);
        }

        return uidLists;
    }

    public UID createCoupon() {
        UID uid = CouponManageUtils.createCoupon(this, this.getData(), this.getId());
        if (Objects.isNull(uid)) {
            return null;
        } else {
            long amount = this.getData().getAmount() + 1L;
            this.getData().withAmount(amount);
            return uid;
        }
    }

    public CouponManageData findData() {
        return this.getData().duplicate();
    }

    private View newCommonTable(DataModes dataModes) {
        PageForm pageForm = this.newFormShell((new CreatePateTypeData()).withType("代金券"), dataModes).getForm();
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), pageForm);
    }

    private FormShell newFormShell(CreatePateTypeData typeData, DataModes dataModes) {
        return CouponManageUtils.newFormShell(typeData, dataModes, this.getData(), CMD_SAVE, CMD_CANCEL, this, "满减券");
    }

    public CouponManageListBean getCouponManageUidDataForCouponManageList(CouponManageListBean bean) {
        return ((AssistCouponManageList)this.getSingleton(AssistCouponManageList.class)).updateBean(this.getId(), this.getData(), bean);
    }

    public boolean timerCall(String s) {
        if (Objects.equals(this.getData().getId() + this.getData().getNumber(), s)) {
            this.getData().withState("已过期");
            List<CouponListBean> beans = ((CouponListImpl)this.getIndex(CouponListImpl.class)).search((new CouponListCriteria()).withCouponManageUidEquals(this.getId().value()), (SortSpec)null, 100000);
            if (beans.size() > 0) {
                CouponData data = (new CouponData()).withState("已过期");
                beans.forEach((e) -> {
                    ((Coupon)this.getUnit(Coupon.class, e.getCouponUid())).update(data);
                });
            }

            return false;
        } else {
            return true;
        }
    }
}
